package com.itheima.restkeeper.face;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.restkeeper.AffixFace;
import com.itheima.restkeeper.DishFace;
import com.itheima.restkeeper.enums.DishEnum;
import com.itheima.restkeeper.exception.ProjectException;
import com.itheima.restkeeper.pojo.Dish;
import com.itheima.restkeeper.pojo.DishFlavor;
import com.itheima.restkeeper.req.AffixVo;
import com.itheima.restkeeper.req.DishVo;
import com.itheima.restkeeper.service.IDishFlavorService;
import com.itheima.restkeeper.service.IDishService;
import com.itheima.restkeeper.utils.BeanConv;
import com.itheima.restkeeper.utils.EmptyUtil;
import com.itheima.restkeeper.utils.ExceptionsUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName DishFaceImpl.java
 * @Description 菜品接口实现
 */
@Slf4j
@DubboService(version = "${dubbo.application.version}",timeout = 5000,
        methods ={
                @Method(name = "findDishVoPage",retries = 2),
                @Method(name = "createDish",retries = 0),
                @Method(name = "updateDish",retries = 0),
                @Method(name = "deleteDish",retries = 0)
        })
public class DishFaceImpl implements DishFace {
      @Autowired
    IDishService dishService;

    @Autowired
    IDishFlavorService dishFlavorService;

    @Autowired
    RedissonClient redissonClient;

    @DubboReference(version = "${dubbo.application.version}",check = false)
    AffixFace affixFace;


    @Override
    public Page<DishVo> findDishVoPage(DishVo dishVo,
                                       int pageNum,
                                       int pageSize)throws ProjectException {
        try {
            //查询菜品分页
            Page<Dish> page = dishService.findDishVoPage(dishVo, pageNum, pageSize);
            //结果集转换
            Page<DishVo> pagevo = new Page<>();
            BeanConv.toBean(page,pagevo);
            List<Dish> dishList = page.getRecords();
            List<DishVo> dishVos = BeanConv.toBeanList(dishList, DishVo.class);
            //处理附件
            if (!EmptyUtil.isNullOrEmpty(dishVos)){
                dishVos.forEach(n->{
                    List<AffixVo> affixVo = affixFace.findAffixVoByBusinessId(n.getId());
                    if (!EmptyUtil.isNullOrEmpty(affixVo)){
                        n.setAffixVo(affixVo.get(0));
                    }
                    List<DishFlavor> id = dishFlavorService.findDishFlavorByDishId(n.getId());
                    ArrayList<String> list = new ArrayList<>();
                    for (DishFlavor dishFlavor : id) {
                        list.add(String.valueOf(dishFlavor.getDataKey()));
                    }
                    String[] strings = new String[list.size()];
                    list.toArray(strings);
                    n.setHasDishFlavor(strings);
                });
            }
            pagevo.setRecords(dishVos);
                    //处理口味
            //返回结果
            return pagevo;
        } catch (Exception e) {
            log.error("查询菜品列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(DishEnum.PAGE_FAIL);
        }
    }

    @Override
    public DishVo createDish(DishVo dishVo) throws ProjectException{
        try {
            //创建菜品
            //构建初始化库存
            //绑定附件
            //数据回显
            return null;
        } catch (Exception e) {
            log.error("保存菜品异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(DishEnum.CREATE_FAIL);
        }
    }

    @Override
    public Boolean updateDish(DishVo dishVo) throws ProjectException {
        //菜品库存锁，前段小程序点餐或后台修改订单项都需要加锁
        //添加可重入锁
        try {
                //修改菜品
                //处理菜品图片
                        //删除图片
                        //绑定新图片
                //构建redis库存
                return null;
        } catch (Exception e) {
            log.warn("修改菜品产生并发dishId:{}",dishVo.getId());
           throw new ProjectException(DishEnum.UPDATE_FAIL);
        }
    }

    @Override
    public Boolean deleteDish(String[] checkedIds)throws ProjectException {
        try {
                //删除图片
            return null;
        } catch (Exception e) {
            log.error("删除菜品异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(DishEnum.DELETE_FAIL);
        }
    }

    @Override
    public DishVo findDishByDishId(Long dishId)throws ProjectException {
        try {
            //按菜品ID查找菜品
            return null;
        } catch (Exception e) {
            log.error("查找菜品所有菜品异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(DishEnum.SELECT_DISH_FAIL);
        }
    }

}
